package org.hrc.puzzleGame.view;

import org.hrc.puzzleGame.constant.GameField;
import org.hrc.puzzleGame.controller.GameController;
import org.hrc.puzzleGame.model.User;
import org.hrc.puzzleGame.util.Globals;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.Collections;

public class GameFrame extends BaseFrame implements GameField {

    private GameController gameController = new GameController();

    //图片路径
    String ImagePath = "image\\animal\\animal1\\";
    //空白的坐标
    int x = 3, y = 3;
    //计算步数
    int count = 0;

    //初始化对象
    public GameFrame() {

        //初始化窗口
        initFrame();
        //初始化菜单
        initJMenu();
        //打乱图片
        shufflePictures();
        //初始化图片
        initPictures();
        //显示窗口
        setVisible(true);
    }

    //初始化窗口
    public void initFrame() {

        setSize(600, 700);
        //将窗口放置到屏幕中间
        setLocationRelativeTo(null);
        setTitle("拼图小游戏 v-1.0");
        //取消中间放置布局
        setLayout(null);
        //点击退出时虚拟机停止运行
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //为整个窗口添加键盘监听
        addKeyListener(this);

    }

    @Override
    public void initWord() {

    }

    @Override
    public void initButton() {

    }

    @Override
    public void initDialog(JLabel jLabel) {

    }


    //初始化菜单栏
    public void initJMenu() {

        GameField.jMenuBar.add(GameField.functionJMenu);
        GameField.jMenuBar.add(GameField.aboutJMenu);

        GameField.functionJMenu.add(GameField.reset);
        GameField.functionJMenu.add(GameField.exit);
        GameField.functionJMenu.add(GameField.ranking);

        GameField.aboutJMenu.add(GameField.author);

        //将菜单栏添加到窗口
        setJMenuBar(GameField.jMenuBar);

        //添加监听
        GameField.reset.addActionListener(this);
        GameField.exit.addActionListener(this);
        GameField.author.addActionListener(this);
        GameField.ranking.addActionListener(this);
    }

    //打乱图片
    public void shufflePictures() {
        //清空集合中图片的名字
        GameField.pos.clear();
        //添加图片的名字
        for (int i = 1; i < 16; i++)
            GameField.pos.add(i);
        //生成胜利的图片位置
        for (int i = 0; i < 15; i++)
            GameField.victoryIndex[i / 4][i % 4] = GameField.pos.get(i);
        //打乱图片名字
        Collections.shuffle(GameField.pos);
        //生成开始时图片的位置
        for (int i = 0; i < 15; i++)
            GameField.index[i / 4][i % 4] = GameField.pos.get(i);

        GameField.index[3][3] = 0;
    }

    //初始化图片
    public void initPictures() {
        //删除隐藏容器的全部内容
        container.removeAll();
        //记录步数
        GameField.stepCount.setBounds(10, 10, 100, 50);
        GameField.stepCount.setText("步数:" + count);
        container.add(GameField.stepCount);
        //判断是否胜利
        if (isVictory()) {
            GameField.winJLabel.setBounds(83, 134, 420, 420);
            getContentPane().add(GameField.winJLabel);
            System.out.println("胜利");
        }
        //加载图片
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                JLabel jLabel = new JLabel(new ImageIcon(ImagePath + GameField.index[i][j] + ".jpg"));
                jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);
                //为图片添加边框
                jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
                container.add(jLabel);
            }
        }
        //添加背景图片
        GameField.backgroundJLabel.setBounds(40, 40, 508, 560);
        container.add(GameField.backgroundJLabel);
        //刷新
        container.repaint();
    }

    //判断是否胜利
    public boolean isVictory() {
        for (int i = 0; i < GameField.index.length; i++)
            for (int j = 0; j < GameField.index[i].length; j++)
                if (GameField.index[i][j] != GameField.victoryIndex[i][j])
                    return false;
        return true;
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {


        int code = e.getKeyCode();
        System.out.println(code);
        if (code == 87 && x != 3) {
            GameField.index[x][y] = GameField.index[x + 1][y];
            GameField.index[x + 1][y] = 0;
            x++;
            count++;
            initPictures();
            System.out.println("向上移动");
        } else if (code == 83 && x != 0) {
            GameField.index[x][y] = GameField.index[x - 1][y];
            GameField.index[x - 1][y] = 0;
            count++;
            x--;
            initPictures();
            System.out.println("向下移动");
        } else if (code == 65 && y != 3) {
            GameField.index[x][y] = GameField.index[x][y + 1];
            GameField.index[x][y + 1] = 0;
            count++;
            y++;
            initPictures();
            System.out.println("向左移动");
        } else if (code == 68 && y != 0) {
            GameField.index[x][y] = GameField.index[x][y - 1];
            GameField.index[x][y - 1] = 0;
            count++;
            y--;
            initPictures();
            System.out.println("向右移动");
        } else if (code == 192) {
            getContentPane().removeAll();
            String path = "image\\animal\\animal1\\all.jpg";
            JLabel jLabel = new JLabel(new ImageIcon(path));
            jLabel.setBounds(83, 134, 420, 420);
            jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
            getContentPane().add(jLabel);
            getContentPane().repaint();
            System.out.println("查看全图");
        }
       else if (code == 72) {
           System.out.println("一键通关");
           int count = 0;
           for (int i = 0; i < index.length; ++i) {
               for (int j = 0; j < index[i].length; ++j) {
                   index[i][j] = ++count;
               }
           }
           index[3][3] = 0;
           // index = new int[][]{
           //         {1,2,3,4},
           //         {5,6,7,8},
           //         {9,10,11,12},
           //         {13,14,15,0}
           // };
           initPictures();
       }
        else {
            System.out.println("无法执行此操作");
        }
        if (isVictory()) {
            User user = Globals.user;
            if (count < user.getStep())
            user.setStep(count);
            try {
                System.out.println(user);
                gameController.step(user);
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int code = e.getKeyCode();
        if (code == 192) {
            initPictures();
            System.out.println("回复原状");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if (obj == GameField.reset) {
            System.out.println("重新开始游戏");
            x = 3;
            y = 3;
            count = 0;
            shufflePictures();
            initPictures();
        } else if (obj == GameField.exit) {
            System.out.println("退出");
            System.exit(0);
        } else if (obj == GameField.author) {
            System.out.println("作者");
            JDialog jDialog = new JDialog();
            jDialog.setTitle(" 欢迎加好友一起讨论改进此游戏");
            JLabel jLabel = new JLabel(new ImageIcon("image\\about.png"));
//            jLabel.setBounds(0,0,250,250);
            jDialog.getContentPane().add(jLabel);
//            jDialog.setAlwaysOnTop(true);
            jDialog.setSize(250, 250);
            jDialog.setLocationRelativeTo(null);
            //必须关掉这个弹窗才可以进行其他操作
            jDialog.setModal(true);
            jDialog.setVisible(true);

        }  else if (obj == GameField.ranking) {
            System.out.println("排行榜");
        }
    }
}



